package group.ocr.Service.Impl;
import group.ocr.Utils.OSSUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import group.ocr.Config.SingleCharactersConfig;
import group.ocr.Mapper.SingleCharactersMapper;
import group.ocr.Service.OCRService;
import group.ocr.Service.SingleCharactersService;
import group.ocr.pojo.singleCharacters;
import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SingleCharactersServiceImpl implements SingleCharactersService {
    @Autowired
    private SingleCharactersMapper singleCharactersMapper;
    @Autowired
    private SingleCharactersConfig singleCharactersConfig;
    @Autowired
    private OCRService ocrService;
    @Autowired
    private OSSUtils ossUtils;
    @Override
    public List<singleCharacters> search(String characterName, String font, String author, String title) {
        return singleCharactersMapper.search(characterName, font, author, title);
    }

    @Override
    public void collectSingleCharacter(int id) {
        singleCharacters SingleCharacter=singleCharactersMapper.selectById(id);
        singleCharactersConfig.addToCollection(SingleCharacter);
    }

    @Override
    public List<singleCharacters> selectAllSingleCharacters() {
        return singleCharactersMapper.selectAllSingleCharacters();
    }

    @Override
    public String saveSingleCharacters(MultipartFile image, String title, String font, String author) throws JSONException, IOException {
        List<String> words = new ArrayList<>();
        List<String> positions = new ArrayList<>();
        // 调用 OCR 服务，得到 OCR 结果 (JSON 格式字符串)
        String OCR = ocrService.imageRecognition(image);
        System.out.println(OCR);
        String fileUrl= ossUtils.upload(image);
        // 将 OCR 结果转换为 JSON 对象
        JSONObject jsonObject = new JSONObject(OCR);
        // 获取 OCR 结果中的 "data" -> "text_lines"
        JSONArray textLines = jsonObject.getJSONObject("data").getJSONArray("text_lines");
        // 遍历每一行的 text_lines
        for (int i = 0; i < textLines.length(); i++) {
            JSONObject textLine = textLines.getJSONObject(i);
            // 获取当前行中的 "words" 数组
            JSONArray wordsArray = textLine.getJSONArray("words");
            // 遍历每个 word
            for (int j = 0; j < wordsArray.length(); j++) {
                JSONObject word = wordsArray.getJSONObject(j);
                // 提取 word 的 text 和 position
                String text = word.getString("text");
                JSONArray positionArray = word.getJSONArray("position");
                // 将 text 和 position 分别添加到对应的列表
                words.add(text);
                positions.add(positionArray.toString()); // 将 position 转换为字符串以便保存
                singleCharactersMapper.saveSingleCharacters(title,text,fileUrl,positionArray.toString(),font,author);
            }
        }
        // 打印出 words 和 positions，检查是否正确提取
        System.out.println("Words: " + words);
        System.out.println("Positions: " + positions);
        return OCR;  // 你可以根据需求返回 OCR 的结果或其他信息
    }
}
